package com.weixin.service;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.weixin.message.resp.TextMessage;
import com.weixin.util.MessageUtil;


/**
 * 核心服务类
 * 
 * @author XiaodongXu
 * @date 2016-11-17
 */
public class CoreService {

	// 日志对象
	private static Logger log = LoggerFactory.getLogger(CoreService.class);
	public static Map <String,String> userLocation = new HashMap<>();
	/**
	 * 处理微信发来的请求并向用户发送消息
	 * 
	 * @param request
	 * @return XML
	 */
	public static String processRequest(HttpServletRequest request) {
		// 用于回复的XML消息
		String respXml = null;
		try {
			// 调用parseXml方法解析请求消息
			Map<String, String> requestMap = MessageUtil.parseXml(request);
			// 配置发送对象
			TextMessage textMessage = new TextMessage();
			textMessage.setToUserName(requestMap.get("FromUserName"));
			textMessage.setFromUserName(requestMap.get("ToUserName"));
			textMessage.setCreateTime(new Date().getTime());
			textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
			// 根据消息类型处理消息
			String returnMessage = null;
			switch (requestMap.get("MsgType")) {
			case MessageUtil.REQ_MESSAGE_TYPE_EVENT:
				returnMessage = dealEventMessage(requestMap);
				break;
			case MessageUtil.REQ_MESSAGE_TYPE_TEXT:	
				 //WeatherUtil.sendMsgTo(System.currentTimeMillis()+"66");
				 if(null!=requestMap.get("Content"))
				 	returnMessage = WeatherService.getWeatherByCityName(requestMap.get("Content"));
				 log.info("输入文本查看天气");
				break;			
			default:
				returnMessage = "发送城市名查看天气预报";
				break;
			}
			if (null != returnMessage) {
				textMessage.setContent(returnMessage);
				respXml = MessageUtil.messageToXml(textMessage);
			} else {
				respXml = "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return respXml;
	}

	/**
	 * 处理事件类消息
	 * 
	 * @param requestMap
	 * @return
	 */
	private static String dealEventMessage(Map<String, String> requestMap) {
		String result = null;
		switch (requestMap.get("Event")) {
		case MessageUtil.EVENT_TYPE_SUBSCRIBE:
			result = "与君初相识，犹如故人归";
			log.info(requestMap.get("FromUserName")+"关注了公众号");
			break;
		case MessageUtil.EVENT_TYPE_LOCATION:
			saveLocation(requestMap);	
			break;	
		case MessageUtil.EVENT_TYPE_CLICK:
			String eventkey = requestMap.get("EventKey");
			String cityName = "";
			if("weather".equals(eventkey))
			{
				String openId = requestMap.get("FromUserName");
				cityName = WeatherService.getCityName(openId);
			}
			else
			{
				cityName = eventkey;
			}
			try {
				if("house".equals(eventkey))
				{
					result = WeatherService.getPageContent();
				}
				else {
					result = WeatherService.getWeatherByCityName(cityName);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;		
		default:
			break;
		}
		return result;
	}
	private static void saveLocation(Map<String, String> requestMap)
	{
		String fromUser = "";
		String location = "";
		String locationDetail = "";
		if(null!=requestMap.get("FromUserName"))
			fromUser = requestMap.get("FromUserName").toString();		
		if(null!= requestMap.get("Latitude")&&null!=requestMap.get("Longitude"))
		{
			String longtitude = requestMap.get("Longitude").toString();
			String latitude = requestMap.get("Latitude").toString();
			String locationJson = WeatherService.getAddressJson(longtitude,latitude);
			location = WeatherService.getCity(locationJson);
			locationDetail = WeatherService.getLocationDetail(locationJson);
			log.info(fromUser+" save location: "+locationDetail);
			//WeatherUtil.sendMsgTo(System.currentTimeMillis()+locationDetail);
		}
		userLocation.put(fromUser, location);	
		
	}
}
